﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace _10_1
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeTreeView();
        }

        private void InitializeTreeView()
        {
            string userDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            TreeNode rootNode = treeView1.Nodes.Add(userDirectory);
            rootNode.Tag = userDirectory;
            PopulateTreeView(rootNode);
        }

        private void PopulateTreeView(TreeNode parentNode)
        {
            string path = parentNode.Tag.ToString();
            try
            {
                string[] directories = Directory.GetDirectories(path);
                foreach (string directory in directories)
                {
                    TreeNode childNode = parentNode.Nodes.Add(Path.GetFileName(directory));
                    childNode.Tag = directory;
                    PopulateTreeView(childNode);
                }
            }
            catch (UnauthorizedAccessException)
            {
                // 这里捕获权限不足异常，不做处理，直接跳过该目录
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error accessing directory: {ex.Message}");
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();
            string selectedPath = e.Node.Tag.ToString();
            try
            {
                string[] directories = Directory.GetDirectories(selectedPath);
                foreach (string directory in directories)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(directory));
                    item.ImageIndex = 0;
                    listView1.Items.Add(item);
                }

                string[] files = Directory.GetFiles(selectedPath);
                foreach (string file in files)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(file));
                    item.ImageIndex = 1;
                    listView1.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error accessing directory: {ex.Message}");
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string selectedItem = listView1.SelectedItems[0].Text;
                string parentPath = treeView1.SelectedNode.Tag.ToString();
                string fullPath = Path.Combine(parentPath, selectedItem);

                if (Path.GetExtension(fullPath).ToLower() == ".xml")
                {
                    try
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(fullPath);

                        StringBuilder xmlInfo = new StringBuilder();
                        ExtractXmlInfo(xmlDoc.DocumentElement, xmlInfo, 0);

                        MessageBox.Show(xmlInfo.ToString(), "XML Information");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Error reading XML file: {ex.Message}");
                    }
                }
            }
        }

        private void ExtractXmlInfo(XmlNode node, StringBuilder info, int indentLevel)
        {
            string indent = new string(' ', indentLevel * 4);
            info.AppendLine($"{indent}Element: {node.Name}");

            if (node.Attributes != null && node.Attributes.Count > 0)
            {
                foreach (XmlAttribute attr in node.Attributes)
                {
                    info.AppendLine($"{indent}  Attribute: {attr.Name} = {attr.Value}");
                }
            }

            if (!string.IsNullOrEmpty(node.InnerText.Trim()))
            {
                info.AppendLine($"{indent}  Text: {node.InnerText.Trim()}");
            }

            foreach (XmlNode childNode in node.ChildNodes)
            {
                ExtractXmlInfo(childNode, info, indentLevel + 1);
            }
        }

    }
}
